package com.zmn.mcc.admin.controller.menu;


import com.google.common.base.Joiner;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.mcc.admin.controller.MccBaseController;
import com.zmn.mcc.business.interfaces.menu.MenuBService;
import com.zmn.mcc.common.constant.MenuConsts;
import com.zmn.mcc.model.entity.menu.McMenu;
import com.zmn.mcc.services.interfaces.menu.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 菜单配置表基础修改Controller
 *
 * @author guoqiao
 * @since 2021-05-28 16:23:56
 */
@Slf4j
@RestController
@RequestMapping("/mcc/menu/modify/")
public class MenuModifyController extends MccBaseController {

    @Resource
    private MenuService menuService;

    @Resource
    private MenuBService menuBService;

    /**
     * 描述: 保存
     *
     * @author guoqiao
     * @since 2021-05-28 16:23:56
     */
    @RequestMapping("/addEdit")
    public ResponseDTO<Integer> addEdit(@RequestBody McMenu menu) {

        Date now = DateUtil.getNow();
        Integer menuId = menu.getMenuId();
        try {
            menu.setUpdater(getStaffName());
            menu.setUpdateTime(now);
            if (NumberUtil.isNullOrZero(menuId)) {
                menu.setStatus(MenuConsts.STATUS_START_KEY);
                menu.setCreater(getStaffName());
                menu.setCreateTime(now);
                menuBService.insert(menu);
            } else {
                menuService.updateByKey(menu);
            }
            return ResponseDTO.success(menu.getMenuId());
        } catch (Exception e) {
            log.error("新增/修改失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 功能描述:修改状态
     *
     * @param ids    菜单id集合
     * @param status 状态
     * @author guoqiao
     * @since 2021-05-28 16:23:56
     */
    @RequestMapping(value = "/updateStatus")
    public ResponseDTO<Boolean> updateStatus(@RequestParam(value = "ids") String ids,
                                             @RequestParam(value = "status") Integer status) {

        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的数据！");
        }
        List<Integer> arrayIds = StringUtil.convertStrsToList(ids);
        try {
            List<String> resultMsg = menuBService.updateStatusBatch(arrayIds, status, getStaffName());
            if (CollectionUtil.isNullOrEmpty(resultMsg)) {
                return ResponseDTO.success();
            }
            // 失败提示
            String wrongMsg = Joiner.on(',').skipNulls().join(resultMsg)+ " 状态修改失败,";
            if (Objects.equals(status, MenuConsts.STATUS_START_KEY)) {
                wrongMsg += "父级菜单未开启！";
            } else {
                wrongMsg += "子级菜单未关闭！";
            }
            return ResponseDTO.fail(wrongMsg);

        } catch (Exception e) {
            log.error("{},{}", e.getMessage(), e);
            return ResponseDTO.fail("修改状态失败");
        }
    }

    /**
     * 功能描述:修改排序
     *
     * @param id 菜单id
     * @param sort   排序
     * @author guoqiao
     * @since 2021-05-28 16:23:56
     */
    @RequestMapping("/updateSort")
    public ResponseDTO<Boolean> updateSort(@RequestParam(value = "id") Integer id,
                                           @RequestParam(value = "sort") Integer sort) {

        if (NumberUtil.isNullOrZero(id)) {
            return ResponseDTO.fail("请选择要更改排序的菜单");
        }
        if (NumberUtil.isNullOrZero(sort)) {
            return ResponseDTO.fail("请设置排序值");
        }
        try {
            McMenu mcMenu = McMenu.builder()
                    .menuId(id)
                    .sort(sort)
                    .updater(getStaffName())
                    .updateTime(DateUtil.getNow())
                    .build();
            menuService.updateByKey(mcMenu);
            return ResponseDTO.success(true, "操作成功！");
        } catch (Exception e) {
            return ResponseDTO.fail("修改排序失败");
        }
    }

}
